package com.ctfo.ulcp.exp.db

import com.mchange.v2.c3p0.ComboPooledDataSource
import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.Transaction
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction


object DSL {

    private val database: Database

    init {
        println("init database DSL")
        val dataSource = ComboPooledDataSource()
        dataSource.jdbcUrl = SYSTEM_JDBC_URL
        dataSource.maxPoolSize = 5
        database =
            Database.connect(dataSource)

    }

    fun <T> transaction(statement: Transaction.() -> T): T =
        org.jetbrains.exposed.sql.transactions.transaction(database, statement)


    suspend fun <T> dbQuery(block: suspend () -> T): T =
        newSuspendedTransaction(Dispatchers.IO) { block() }


}